/*
 * @Author: szx
 * @Date: 2022-03-20 15:26:59
 * @LastEditTime: 2022-03-20 15:37:12
 * @Description:
 * @FilePath: \leetcode\700-799\714\714_2.js
 */
/**
 * @param {number[]} prices
 * @param {number} fee
 * @return {number}
 */
var maxProfit = function (prices, fee) {
    let res = 0;
    let minPrice = prices[0];
    for (let i = 1; i < prices.length; i++) {
        if (prices[i] < minPrice) {
            minPrice = prices[i];
        }
        if (prices[i] > minPrice + fee) {
            res += prices[i] - minPrice - fee;
            minPrice = prices[i] - fee;
        }
    }
    return res;
};
console.log(maxProfit([1, 3, 2, 8, 4, 9], 2));
console.log(maxProfit([1, 3, 7, 5, 10, 3], 3));
